<template>
  <div class="screen">
    <el-row>
      <el-col :span="20">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="影院名称">
            <el-select
              v-model="formInline.cinemaName"
              placeholder="影院名称"
              clearable
              @change="getScreenType"
            >
              <el-option
                v-for="item in allCinemas"
                :key="item.id"
                :label="item.cinemaName"
                :value="item.cinemaName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="影厅名称">
            <el-input
              v-model="formInline.screenName"
              placeholder="影厅名称"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryPanel">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4" style="text-align: right">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="dialogVisible = true"
        >添加影厅</el-button>
      </el-col>
    </el-row>
    <div class="table">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" width="50" />
        <el-table-column prop="cinemaName" label="影院名称" width="280" />
        <el-table-column prop="screenName" label="影厅名称" width="220" />
        <el-table-column prop="screenType" label="影厅类型" width="300">
          <template slot-scope="scope">
            <el-tag v-for="(item, i) in scope.row.screenType" :key="i">{{
              item
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="note" label="说明" />
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-link
              type="primary"
              :underline="false"
              style="margin-right: 15px"
              @click="edit(scope.row)"
            >
              编辑
            </el-link>
            <el-link
              type="danger"
              :underline="false"
              @click="remove(scope.row.id)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      :title="isEdit ? '编辑影厅' : '添加影厅'"
      :visible.sync="dialogVisible"
      width="40%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="true"
      :before-close="close"
    >
      <div style="padding: 10px 50px">
        <el-form
          ref="screenDataRef"
          :model="screenData"
          :rules="screenDataRules"
          label-width="90px"
          class="demo-ruleForm"
          label-position="left"
        >
          <el-form-item label="影院名称" prop="cinemaName">
            <el-select
              v-model="screenData.cinemaName"
              placeholder="请选择影院"
              @change="getScreenType"
            >
              <el-option
                v-for="item in allCinemas"
                :key="item.id"
                :label="item.cinemaName"
                :value="item.cinemaName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="影厅名称" prop="screenName">
            <el-input v-model="screenData.screenName" />
          </el-form-item>
          <el-form-item label="影厅类型" prop="screenType">
            <el-select
              v-model="screenData.screenType"
              multiple
              placeholder="请选择影厅类型"
            >
              <el-option
                v-for="(item, i) in screenType"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="说明" prop="note">
            <el-input v-model="screenData.note" rows="4" type="textarea" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          @click="save('screenDataRef')"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Screen',

  data() {
    return {
      isEdit: false,
      dialogVisible: false,
      formInline: {
        cinemaName: '',
        screenName: ''
      },
      screenList: [
        {
          id: '1',
          cinemaName: '万达影城（高新区店）',
          screenName: '1号厅',
          screenType: ['巨幕厅', '4D厅'],
          note: '3D眼镜建议自备或购买'
        },
        {
          id: '2',
          cinemaName: '中影星美国际影城（闽侯店）',
          screenName: '2号厅',
          screenType: ['杜比全景声厅', '4D厅'],
          note: 'VIP厅-儿童需购票，赠送零食和饮料	'
        }
      ],
      tableData: [],
      allCinemas: [],
      screenData: {
        id: new Date().getTime(),
        cinemaName: '',
        screenName: '',
        screenType: [],
        note: ''
      },
      screenType: [],
      screenDataRules: {
        cinemaName: [
          { required: true, message: '请选择影院名称', trigger: 'blur' }
        ],
        screenName: [
          { required: true, message: '请输入影厅地址', trigger: 'blur' }
        ],
        screenType: [
          { required: true, message: '请选择影院类型', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const allScreen = await this.$localStorage.getItem('allScreen')
    if (!allScreen) {
      await this.$localStorage.setItem('allScreen', this.screenList)
    }
  },
  async mounted() {
    const allCinemas = await this.$localStorage.getItem('allCinemas')
    this.allCinemas = allCinemas
    this.tableData = await this.$localStorage.getItem('allScreen')
    // console.log(this.allCinemas);
  },

  methods: {
    async queryPanel() {
      const allScreen = await this.$localStorage.getItem('allScreen')

      const { cinemaName, screenName } = this.formInline

      const newData = allScreen.filter((item) => {
        if (
          item.cinemaName.includes(cinemaName) &&
          item.screenName.includes(screenName)
        ) {
          return item
        }
      })

      this.tableData = newData
      // console.log(movieName, movieType, hotnew);
    },
    close() {
      this.screenData = {
        id: new Date().getTime(),
        cinemaName: '',
        screenName: '',
        screenType: [],
        note: ''
      }
      this.dialogVisible = false
      this.isEdit = false
    },
    edit(data) {
      this.isEdit = true
      this.screenData = Object.assign({}, data)
      this.getScreenType(data.cinemaName, true)
      this.dialogVisible = true
    },
    remove(id) {
      this.$confirm('此操作将删除该影厅, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const newData = this.tableData.filter((item) => item.id !== id)
          this.tableData = newData
          await this.$localStorage.setItem('allScreen', newData)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {})
    },
    save(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          if (this.isEdit) {
            const newData = this.tableData.map((item) => {
              if (item.id === this.screenData.id) {
                return this.screenData
              }
              return item
            })
            this.tableData = newData
            await this.$localStorage.setItem('allScreen', this.tableData)
          } else {
            this.tableData.unshift(this.screenData)
            await this.$localStorage.setItem('allScreen', this.tableData)
          }
          this.close()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getScreenType(val, bool) {
      this.screenType = []
      if (!bool) this.screenData.screenType = []
      this.allCinemas.find((item) => {
        if (val === item.cinemaName) {
          return (this.screenType = item.cinemaType)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.screen {
  padding: 20px 100px;
  .el-tag {
    margin-right: 10px;
    margin-bottom: 5px;
  }
}
.el-select {
  width: 100%;
}
.el-date-editor {
  width: 100%;
}
.el-cascader {
  width: 100%;
}
</style>
